package com.sunnada.demo.service.serviceimpl;

import java.util.List;
import java.util.regex.Pattern;

import com.sunnada.demo.bean.Dog;
import com.sunnada.demo.service.DogService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

/**
 * @program: mybatisproject2
 * @description: mongodbTemplate使用
 * @author: 李瑞益
 * @create: 2018-10-31 10:24
 **/
@Service
public class DogServiceImplTemplate implements DogService {

    @Autowired
    private MongoTemplate mongoTemplate;
    @Override
    public void delete(String id) {
        Query query = new Query(Criteria.where("id").is(id));
        Dog dog = new Dog();
        dog.setId(id);
//        DeleteResult remove = mongoTemplate.remove(query);
        mongoTemplate.remove(dog);
    }

    @Override
    public Dog insert(Dog dog) {
        dog.setId(null);
        mongoTemplate.insert(dog);
        return null;
    }

    @Override
    public Dog update(Dog dog) {

        Query query = new Query(Criteria.where("id").is(dog.getId()));
        Update update = new Update().set("name",dog.getName()).set("age",dog.getAge());
        //更新查询出的结果的第一条
        mongoTemplate.updateFirst(query,update,Dog.class);
        //更新所有
        mongoTemplate.updateMulti(query,update,Dog.class);
        return null;
    }

    @Override
    public Dog getById(String id) {
        Query query = new Query(Criteria.where("id").is(id));
       Dog dogs = mongoTemplate.findOne(query, Dog.class);
        return dogs;
    }

    @Override
    public List<Dog> getList() {
        return mongoTemplate.findAll(Dog.class);
    }

    @Override
    public List<Dog> findByName(String name) {
       Query query = new Query(Criteria.where("userName").is(name));
        List<Dog> trackLogs = mongoTemplate.find(query, Dog.class);
        return trackLogs;
    }

    @Override
    public List<Dog> findByIdAndName(Object id, String name) {
        Pattern p = Pattern.compile(".*"+name+".*",Pattern.CASE_INSENSITIVE);
        Query query = new Query();
        Criteria criteria ;
        if(id != null && !"".equals(id)){
            query.addCriteria(Criteria.where("id").is(id));
        }
        if(name != null && !"".equals(name)){
            query.addCriteria(Criteria.where("name").regex(name));
        }
        List<Dog> dogs = mongoTemplate.find(query, Dog.class);
        return dogs;
    }

    @Override
    public List<Dog> findByNameAndAge(String name, String age) {
        Pattern p = Pattern.compile(".*"+name+".*",Pattern.CASE_INSENSITIVE);
        Query query = new Query();
        if(name != null && !"".equals(name)){
            query.addCriteria(Criteria.where("name").regex(p));
        }
        if(age != null && !"".equals(age)){
            query.addCriteria((Criteria.where("age").is(age)));
        }
        List<Dog> dogs = mongoTemplate.find(query, Dog.class);
        return dogs;
    }
}
